package com.mjk.common.util;


import com.mjk.common.tools.logger.BizLogger;

import java.io.*;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * 资源文件工具类
 * @author healy
 *
 */
public class ResourceUtils extends Properties{

	private static final long serialVersionUID = -5668734698503528270L;

	private final ResourceBundle resourceBundle;
	
	private ResourceUtils(String resource) {
		resourceBundle = ResourceBundle.getBundle(resource);
	}
	
	/**
	 * 获取资源
	 * @param resource 资源
	 * @return 解析
	 */
	public static ResourceUtils getResource(String resource) {
		return new ResourceUtils(resource);
	}
	
	/**
	 * 根据key取得value
	 * @param key 键值
	 * @param args value中参数序列，参数:{0},{1}...,{n}
	 * @return
	 */
	public String getValue(String key, Object... args) {
		String temp = resourceBundle.getString(key);
		return MessageFormat.format(temp, args);
	}
	
	/**
	 * 获取所有资源的Map表示
	 * @return 资源Map
	 */
	public Map<String, String> getMap() {
		Map<String, String> map = new HashMap<>();
		for(String key: resourceBundle.keySet()) {
			map.put(key, resourceBundle.getString(key));
		}
		return map;
	}
	/**
	 * 获取配置文件信息
	 * @return
	 */
	public static String getApplicationName(){
		try{
			String profile = ResourceUtils.getResource("bootstrap").getMap().get("spring.profiles.active");
			if(ObjectUtils.isEmpty(profile)){
				return "application";
			}else{
				return "application."+profile;
			}
		}catch(Exception e){
			return "application";
		}
	}
	public void load(String resourcePath) {
		InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(resourcePath);
		load(inputStream);
		
		String coustemFile = System.getProperty(resourcePath);
		if (!ObjectUtils.isEmpty(coustemFile)) {
			InputStream coustemProperties = this.getClass().getClassLoader().getResourceAsStream(coustemFile);
			if (coustemProperties != null) {
				load(coustemProperties);
			}
		} else {
			coustemFile = System.getProperty(resourcePath);
			if (!ObjectUtils.isEmpty(coustemFile)) {
				InputStream coustemProperties = this.getClass().getClassLoader().getResourceAsStream(coustemFile);
				if (coustemProperties == null) {
					try {
						coustemProperties = new FileInputStream(coustemFile);
					} catch (FileNotFoundException e) {
						BizLogger.error(e);
					}
				}
				if (coustemProperties != null) {
					load(coustemProperties);
				}
			}
		}
		loadDefaultFromSystem();
	}

	@Override
	public synchronized void loadFromXML(InputStream inputStream) {
		try {
			super.loadFromXML(inputStream);
		} catch (IOException e) {
			BizLogger.error(e);
		}
	}

	@Override
	public synchronized void load(InputStream inputStream) {
		try {
			super.load(inputStream);
		} catch (IOException e) {
			BizLogger.error(e);
		}
	}

	@Override
	public synchronized void load(Reader reader) {
		try {
			super.load(reader);
		} catch (IOException e) {
			BizLogger.error(e);
		}
	}

	private void loadDefaultFromSystem() {
		Properties p = System.getProperties();
		this.putAll(p);
		this.putAll(System.getenv());
	}
}
